import { storage } from './storage'

const TOKEN_KEY = 'ACCESS_TOKEN'
const REFRESH_TOKEN_KEY = 'REFRESH_TOKEN'
const USER_INFO_KEY = 'USER_INFO'

// Token相关操作
export function getToken() {
  return storage.get(TOKEN_KEY)
}

export function setToken(token: string) {
  return storage.set(TOKEN_KEY, token)
}

export function removeToken() {
  return storage.remove(TOKEN_KEY)
}

// RefreshToken相关操作
export function getRefreshToken() {
  return storage.get(REFRESH_TOKEN_KEY)
}

export function setRefreshToken(token: string) {
  return storage.set(REFRESH_TOKEN_KEY, token)
}

export function removeRefreshToken() {
  return storage.remove(REFRESH_TOKEN_KEY)
}

// 用户信息相关操作
export function getUserInfo() {
  const userInfo = storage.get(USER_INFO_KEY)
  return userInfo ? JSON.parse(userInfo) : null
}

export function setUserInfo(info: any) {
  return storage.set(USER_INFO_KEY, JSON.stringify(info))
}

export function removeUserInfo() {
  return storage.remove(USER_INFO_KEY)
}

// 清除所有认证信息
export function clearAuthInfo() {
  removeToken()
  removeRefreshToken()
  removeUserInfo()
}

// 检查是否已登录
export function isLoggedIn() {
  return !!getToken()
}
